home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / Macintosh Tracker 1.1 Source / Original Tracker 3.10 Source / termio.c < prev    next >
Text File  |  1993-05-22  |  2KB  |  128 lines

  1. /* termio.c */
  2. /* special termio discipline for sun/sgi,
  3.  * for non blocking io and such.
  4.  * These functions should not be too difficult
  5.  * to write for a PC.
  6.  */
  7.  
  8. /* $Id: termio.c,v 3.8 1993/04/25 14:50:17 espie Exp espie $
  9.  * $Log: termio.c,v $
  10.  * Revision 3.8  1993/04/25  14:50:17  espie
  11.  * cflags interpreted correctly.
  12.  *
  13.  * Revision 3.7  1993/01/16  16:23:33  espie
  14.  * Hsavolai fix.
  15.  *
  16.  * Revision 3.6  1993/01/15  14:00:28  espie
  17.  * Added bg/fg test.
  18.  *
  19.  * Revision 3.5  1993/01/06  17:58:39  espie
  20.  * Added changes for linux.
  21.  *
  22.  * Revision 3.4  1992/12/03  15:00:50  espie
  23.  * restore stty.
  24.  *
  25.  * Revision 3.3  1992/11/27  10:29:00  espie
  26.  * General cleanup
  27.  *
  28.  * Revision 3.2  1992/11/22  17:20:01  espie
  29.  * Added update_frequency call, mostly unchecked
  30.  *
  31.  * Revision 3.1  1992/11/19  20:44:47  espie
  32.  * Protracker commands.
  33.  *
  34.  */
  35.  
  36. #ifdef linux
  37. #include <termios.h>
  38. #else
  39. #include <sys/termio.h>
  40. #endif
  41. #include <stdio.h>
  42. #include <signal.h>
  43. #include "defs.h"
  44.  
  45. LOCAL struct termio sanity;
  46.  
  47. LOCAL BOOL is_fg;
  48.  
  49. /* signal handler */
  50.  
  51. LOCAL void goodbye(sig)
  52. int sig;
  53.     {
  54.     fprintf(stderr, "\nSignal %d", sig);
  55.     discard_buffer();
  56.     end_all();
  57.     }
  58.  
  59. BOOL run_in_fg()
  60.     {
  61.     int val;
  62.     /* real check for running in foreground */
  63.     if (ioctl(fileno(stdin), TIOCGPGRP, &val))
  64.         return FALSE; 
  65.     if (val == getpgrp())
  66.         return TRUE;
  67.     else
  68.         return FALSE;
  69.     }
  70.  
  71. LOCAL void switch_mode()
  72.     {
  73.     struct termio zap;
  74.  
  75.     signal(SIGCONT, switch_mode);
  76.     signal(2, goodbye);
  77.     signal(3, goodbye);
  78.  
  79.     if (run_in_fg())
  80.         {
  81.         ioctl(fileno(stdin), TCGETA, &zap);
  82. #ifdef linux
  83.         zap.c_cc[VMIN] = 0;
  84.         zap.c_cc[VTIME] = 0;
  85. /* Commented out
  86. As Hannu said:
  87. The current Linux kernel interprets correctly the c_lflags field so it
  88. should be set like for the other systems. 
  89.         zap.c_lflag = 0;
  90.  */
  91. #else
  92.         zap.c_cc[VEOL] = 0;
  93.         zap.c_cc[VEOF] = 0;
  94.         zap.c_lflag &= ~(ICANON | ECHO);
  95. #endif
  96.         ioctl(fileno(stdin), TCSETA, &zap);
  97.         is_fg = TRUE;
  98.         }
  99.     else
  100.         is_fg = FALSE;
  101.     }
  102.  
  103. void nonblocking_io()
  104.     {
  105.     /* try to renice our own process to get more cpu time */
  106.     nice(-15);
  107.     ioctl(fileno(stdin), TCGETA, &sanity);
  108.     switch_mode();
  109.     }
  110.  
  111.  
  112. void sane_tty()
  113.     {
  114.     ioctl(fileno(stdin), TCSETA, &sanity);
  115.     }
  116.  
  117. int may_getchar()
  118.     {
  119.     char buffer;
  120.  
  121.     if (run_in_fg() && !is_fg)
  122.         switch_mode();
  123.     if (run_in_fg() && read(fileno(stdin), &buffer, 1))
  124.         return buffer;
  125.     return EOF;
  126.     }
  127.  
  128.